package com.baize.baizeapp.test.album

import android.content.Context
import android.content.Intent
import android.media.MediaExtractor
import android.media.MediaFormat
import android.media.MediaMuxer
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.MediaController
import androidx.appcompat.app.AppCompatActivity
import com.baize.baizeapp.R

class PreViewActivity : AppCompatActivity() {
    companion object {
        fun open(context: Context, videoPath: String) {
            val intent = Intent(context, PreViewActivity::class.java)
            intent.putExtra("video_path", videoPath)
            context.startActivity(intent)
        }
    }

    val videoPath: String by lazy {
        intent.getStringExtra("video_path")
    }
    lateinit var videoView: HeiShiVideoView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pre_view)
        videoView = findViewById<HeiShiVideoView>(R.id.videoView)
        videoView.setOnCompletionListener {
            videoView.seekTo(0)
            videoView.start()
        }
        val mediaController = MediaController(this)
        videoView.setVideoPath(videoPath)
        videoView.setMediaController(mediaController)
        videoView.seekTo(0)
        videoView.requestFocus()
        Log.i("yanze", "onCreate: 是否是H265: ${isH265VideoEncoding()}")
    }

    fun isH265VideoEncoding() : Boolean{
        val mVideoExtractor = MediaExtractor()
        mVideoExtractor.setDataSource(videoPath)
        for (i in 0 until mVideoExtractor.trackCount) {
            val format = mVideoExtractor.getTrackFormat(i)
            val encoding = format.getString(MediaFormat.KEY_MIME)
            if (encoding.startsWith("video/")) {
                return TextUtils.equals("video/hevc", encoding)
            }
        }
        return false
    }

    override fun onResume() {
        super.onResume()
        videoView.start()
    }

    override fun onPause() {
        super.onPause()
        videoView.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        videoView.stopPlayback()
    }

}